/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.injectless_mocking;

import picounit.features.classloader.mutator.Access;
import picounit.features.classloader.mutator.Method;
import picounit.features.classloader.mutator.MethodMutator;

public class InjectlessMockingMethodMutator extends MethodMutator {
	public InjectlessMockingMethodMutator(SignatureMangler signatureMangler) {
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Protected methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	protected boolean shouldMutate(Method method) {
		return publicButNotStatic(method.access());
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	private boolean publicButNotStatic(Access access) {
		return access.isPublic() && !access.isStatic();
	}
	
	public void voidNoParamMethod_noInterception() {
		doSomethingElse();
	}

	public static boolean voidNoParamMethod;
	public void voidNoParamMethod() {
		if (voidNoParamMethod) {
			InterceptionPoint.intercept(123, null);
			return; 
		}
		
		doSomethingElse();
	}

	
	private void doSomethingElse() {
	}

	public static boolean voidSomeParamMethod;
	public void voidSomeParamMethod(int value, String anotherValue) {
		if (voidSomeParamMethod) {
			InterceptionPoint.intercept(124, value, anotherValue);
			
			return;
		}
		
		doSomethingElse();
	}

	public static boolean intNoParamMethod;
	public int intNoParamMethod() {
		if (intNoParamMethod) {
			return InterceptionPoint.intercept(125, null);
		}
		return someOtherMethod();
	}

	public int intNoParamMethod_noInterception() {
		return someOtherMethod();
	}
	
	public static boolean integerNoParamMethod;
	public Integer integerNoParamMethod() {
		if (intNoParamMethod) {
			return InterceptionPoint.intercept(125, null);
		}
		return someOtherMethod2();
	}
	

	private Integer someOtherMethod2() {
		return new Integer(2);
	}

	private int someOtherMethod() {
		return 0;
	}

}


class InterceptionPoint {
	@SuppressWarnings("unused")
	public static <T> T intercept(int intereptionPoint, Object ... params) {
		return null;
	}
}

